/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ucjava.chat.commserve.rmi.server;

import com.ucjava.chat.admanager.AdvertisingImpl;
import com.ucjava.chat.admanager.IAdvertising;
import com.ucjava.chat.commserve.rmi.common.IChatService;
import com.ucjava.chat.commserve.rmi.common.OwnIP;
import com.ucjava.chat.commserve.rmi.common.PrefStore;
import java.io.File;
import java.net.SocketException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tony
 */
public class Main
{
    // Property strings:
    //        private static final String SERVICE_NAME = "ChatServer";

    private static final String CODEBASE_DIRECTORY =
            "file:" + "build" + File.separator + "classes" + File.separator;
    
    //private static final String POLICY_FILE =
    //        "server.policy";
    
    private static void initProperties()
    {
        // Set location of policy file:
        //System.getProperties().setProperty("java.security.policy", POLICY_FILE);
        // And codebase:
        System.getProperties().setProperty("java.rmi.server.codebase", CODEBASE_DIRECTORY);
    }
    
   

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        
        // Moved to ChatServiceImpl#startRegistry():
        //Runs the RMIRegistry executable.
        //String cmd =  "start rmiregistry.exe" ;
        //try
        //{
        //    Process p = Runtime.getRuntime().exec(cmd);
        //}
        //catch (IOException ex)
        //{
        //      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        //      System.out.println("File RMIRegistry could not be found.");
        //      System.exit(1); 
        //}
                           
        // local copy of pref/config object:
        PrefStore prefStore = PrefStore.makePrefStore();
        OwnIP ownIP;
        try
          {
            ownIP = OwnIP.buildOwnIP();
          } catch (SocketException ex)
          {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
              System.out.println("Could not determine our IP!");
              System.exit(1);
          }
        initProperties();
        if (System.getSecurityManager() == null)
          {
            System.setSecurityManager(new SecurityManager());
          }
       
          try {
            //intialzise AdvertisingImpl constructor
            IAdvertising advertising = new AdvertisingImpl();
            IChatService chatService = new ChatServiceImpl();
             System.out.println(prefStore.getServerReadyMsg());

            // TODO code application logic here
          } catch (RemoteException e)
          {
            System.err.println("Remote Exception in Server:");
            e.printStackTrace();
            System.exit(3);
          } 
        }
    }

